package edu.famework.demo5;

import java.util.Scanner;

public class TestBook {
    /**
     * 输出图书的信息，参数为数组
     * @param books
     */
    public void printBookInfo(Book[] books){
        //遍历输出
        for (int i = 0; i < books.length; i++) {
            Book b = books[1];
            System.out.println(b.getBid()+"--"+b.getBname()+"--"+b.getStorage());
        }
    }

    /**
     *
     * @param books 对象
     * @return order 返回一个对象
     */
    public Order buyBooks(Book[]books){
        //1.创建一个订单
        Order order = new Order("0001");
        //2.创建三次接受用户的输入
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入图书的编号：");
            int pid = input.nextInt();
            System.out.println("请输入购买的数量：");
            int num = input.nextInt();
            //3.将购买的商品保存到数组（orderItem[] items）中
            OrderItem orderItem = new OrderItem(books[pid-1].getBname(),books[pid-1].getPrice(),num);
            order.setItems(orderItem,i);
        }
        return order;
    }

    public void printOrder(Order order){
        System.out.println(order.getOrderId());
        System.out.println(order.getTotal());
    }

    public static void main(String[] args) {
        Book[] books = new Book[3];
        Book book1 = new Book(1,"java1",33.45,50);
        Book book2 = new Book(2,"java2",33.55,10);
        Book book3 = new Book(3,"java3",33.65,30);
        //将对象存入数组中
        books[0] = book1;
        books[1] = book2;
        books[2] = book3;

        TestBook testBook = new TestBook();
        testBook.printBookInfo(books);

        //1.创建一个订单
        //2.循环三次接受用户输入
        //3.将购买的商品保存到数组中
        Order order = testBook.buyBooks(books);

        //输出订单信息
        testBook.printOrder(order);

    }

}
